<template>
  <a-layout>
    <!-- 侧边栏 -->
    <a-layout-sider width="200" style="background: #fff">
      <a-menu mode="inline" :style="{ height: '100%', borderRight: 0 }" @click="handleClick" :openKeys="openKeys">
        <a-menu-item key="welcome">
          <MailOutlined />
          <span>欢迎</span>
        </a-menu-item>
        <a-sub-menu v-for="item in level1" :key="item.id">
          <template v-slot:title>
            <span><user-outlined />{{ item.name }}</span>
          </template>
          <a-menu-item v-for="child in item.children" :key="child.id">
            <MailOutlined /><span>{{ child.name }}</span>
          </a-menu-item>
        </a-sub-menu>
        <a-menu-item key="tip">
          <span>以上菜单在分类管理配置</span>
        </a-menu-item>
      </a-menu>
    </a-layout-sider>
    <!-- 主体区域 -->
    <a-layout-content :style="{
      background: '#fff',
      padding: '24px',
      margin: 0,
      minHeight: '280px',
    }">
      <!-- 欢迎界面 -->
      <div class="welcome" v-show="isShowWelcome">
        <h1>欢迎来到阿远的学习天地!</h1>
      </div>
      <!-- 电子书列表展示区域 -->
      <a-list v-show="!isShowWelcome" item-layout="vertical" size="large" :data-source="ebooks"
        :grid="{ gutter: 20, column: 3 }">
        <!-- grid把一行变成三列 每列的间距是20px -->
        <template #renderItem="{ item }">
          <a-list-item key="item.name">
            <template #actions>
              <span v-for="{ type, text } in actions" :key="type">
                <component v-bind:is="type" style="margin-right: 8px" />
                {{ text }}
              </span>
            </template>

            <a-list-item-meta :description="item.description">
              <template #title>
                <router-link :to="`/doc?ebookId=${item.id}`">{{ item.name }}</router-link>
              </template>
              <template #avatar><a-avatar :src="item.cover" /></template>
            </a-list-item-meta>
          </a-list-item>
        </template>
      </a-list>
    </a-layout-content>
  </a-layout>
</template>

<script lang="ts">
import { defineComponent, onMounted, reactive, ref, toRef } from "vue";
import axios from "axios";
import { message } from 'ant-design-vue';
import { Tool } from "@/util/tool";

// const listData: Record<string, string>[] = [];

// for (let i = 0; i < 23; i++) {
//   listData.push({
//     href: "https://www.antdv.com/",
//     title: `ant design vue part ${i}`,
//     avatar: "https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png",
//     description:
//       "Ant Design, a design language for background applications, is refined by Ant UED Team.",
//     content:
//       "We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.",
//   });
// }

export default defineComponent({
  name: "Home",
  setup() {
    // console.log("setup");
    const ebooks = ref();
    //const ebooks1 = reactive({ books: [] });

    //显示欢迎和电子书分类
    const isShowWelcome = ref(true);
    let categoryId2 = 0;

    // 树型菜单
    const level1 = ref();
    let categorys: any;

    const openKeys = ref();
    /**
     * 查询所有分类
     **/
    const handleQueryCategory = () => {
      axios.get("/category/all").then((response) => {
        const data = response.data;
        if (data.success) {
          categorys = data.content;
          console.log("原始数组：", categorys);

          // 加载完分类后，将侧边栏全部展开
          openKeys.value = [];
          for (let i = 0; i < categorys.length; i++) {
            openKeys.value.push(categorys[i].id)
          }

          level1.value = [];
          level1.value = Tool.array2Tree(categorys, 0);
          console.log("树形结构：", level1.value);
        } else {
          message.error(data.message);
        }
      });
    };

    const handleQueryEbook = () => {
      axios
        .get("/ebook/list", {
          params: {
            page: 1,
            size: 1000,
            categoryId2: categoryId2
          },
        })
        .then((res) => {
          // console.log(res);
          const data = res.data;
          ebooks.value = data.content.list;
          // ebooks1.books = data.content;
        });
    };


    // value参数自带 具体查阅文档
    const handleClick = (value: any) => {
      console.log("menu click", value);
      //isShowWelcome.value = value.key === 'welcome';
      if (value.key === 'welcome') {
        isShowWelcome.value = true;
      } else {
        categoryId2 = value.key;
        isShowWelcome.value = false;
        handleQueryEbook();
      }

    };







    // 阅读数 点赞数 收藏数
    const actions: Record<string, string>[] = [
      { type: "StarOutlined", text: "156" },
      { type: "LikeOutlined", text: "156" },
      { type: "MessageOutlined", text: "2" },
    ];

    const pagination = ref({
      pageSize: 3,
    });

    onMounted(() => {
      // console.log("onMounted");
      //初始加载所有分类
      handleQueryCategory();


    });

    return {
      ebooks,
      pagination,
      actions,
      // ebooks2: toRef(ebooks1, "books"),

      handleClick,
      handleQueryEbook,
      level1,


      isShowWelcome,
      openKeys
    };
  },
});
</script>



<style scoped>
.ant-avatar {
  width: 50px;
  height: 50px;
  line-height: 50px;
  border-radius: 8%;
  margin: 5px 0;
}
</style>